//
//  VideoHandlerModel.m
//  VideoExam
//
//  Created by apple on 2020/6/18.
//

#import "VideoHandlerModel.h"
#import "VideoProcessing.h"

@interface VideoHandlerModel ()
///原文件路径
@property(nonatomic,strong)NSString * original_path;

///裁剪后的路径
@property(nonatomic,strong)NSString * tailoring_path;
///最后处理完成的path
@property(nonatomic,strong)NSString * share_path;
/// 裁剪开始时间
@property(nonatomic,assign)CGFloat tailoring_start_time;
///裁剪结束时间
@property(nonatomic,assign)CGFloat tailoring_end_time;

@end
@implementation VideoHandlerModel
- (instancetype)init {
    self = [super init];
    if (self) {
        _tailoring_start_time = 0;
        _tailoring_end_time = 0;
    }
    return self;
}


+ (instancetype)init_with_video_url_path:(NSString *)url_path {
    VideoHandlerModel * handlerModel = [[VideoHandlerModel alloc] init];
    handlerModel.original_path = url_path;
    return handlerModel;
}
- (void)video_handler_completion:(void(^)(NSString * path))completion {
    NSURL *url = [NSURL fileURLWithPath:self.original_path];
    AVAsset * asset = [AVAsset assetWithURL:url];
   
    dispatch_queue_t queue = dispatch_queue_create("myQueu", DISPATCH_QUEUE_SERIAL);
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    
    if ([self is_tailoring_video]) { //是否截取视频
         self.tailoring_path = [self get_new_path];
         dispatch_async(queue, ^{
               [VideoProcessing cutVideoWithAVAsset:asset startTime:self.tailoring_start_time endTime:self.tailoring_end_time filePath:self.tailoring_path completion:^(NSURL * _Nonnull outputURL) {
                      NSLog(@"视频截取完成 %@",outputURL);
                    dispatch_semaphore_signal(semaphore);
               } errorBlock:^{
                   dispatch_semaphore_signal(semaphore);
               }];
               dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
           });
    } else {
        //如果不裁剪 就把原路径 赋值给裁剪路径
        self.tailoring_path = self.original_path;
    }
    
   
     dispatch_async(queue, ^{
         self.share_path =[self get_new_path];//获取一个新的路径
          [VideoProcessing addWaterPicWithVideoPath:self.tailoring_path outputPath:self.share_path completion:^(NSURL * _Nonnull outputURL) {
             NSLog(@"水印添加完成 %@",outputURL);
              dispatch_semaphore_signal(semaphore);
          } errorBlock:^{
              dispatch_semaphore_signal(semaphore);
          }];
          dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
     });
 
    dispatch_async(queue, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion) {
                  completion(self.share_path);
              }
        });
    });
 
}
- (BOOL)is_tailoring_video {
    //是否截取是视频规则 如果视频 小于30秒则不截取 直接 添加水印 分享 大于30秒切 到了60秒 的截取 30 - 60秒的 ， 如果大于30秒 切不到60秒就截取 30 到结束的
   NSInteger seconds = [VideoHandlerModel get_video_time_url_path:self.original_path];
    if (seconds <= 30) {
        return NO;
    } else if (seconds > 30 && seconds < 60) {
       self.tailoring_start_time = 30;
       self.tailoring_end_time = seconds;
       return YES;
    } else if (seconds >= 60) {
        self.tailoring_start_time = 30;
        self.tailoring_end_time = 60;
        return YES;
    }
    return NO;
}
- (NSString *)get_new_path {
    NSString * path = @"";
    if ([FileManagement create_document_folderName:share_video_file_folder_name]) {
          path = [FileManagement get_Folder_specified_path_fileFolderName:share_video_file_folder_name];
        NSString * date_name = [self get_date_str];
           path=[NSString stringWithFormat:@"%@/%@.mp4",path,date_name];
       return path;
    }
    return path;
}
+ (NSInteger)get_video_time_url_path:(NSString *)url_path {
     NSURL*videoUrl = [NSURL fileURLWithPath:url_path];
    AVURLAsset *avUrl = [AVURLAsset assetWithURL:videoUrl];
    CMTime time = [avUrl duration];
    NSInteger seconds = ceil(time.value/time.timescale);
    NSLog(@"视频时长 %ld",seconds);
    return seconds;
}


///删除本地文件无回调
+ (void)delete_video_File_path:(NSString *)file_path {
  BOOL isSuccess = NO;
  NSError *error;
  NSFileManager *fileManager = [NSFileManager defaultManager];
  isSuccess = [fileManager removeItemAtPath:file_path error:&error];
  if (error) {
      NSLog(@"removeFile 失败：%@",[error localizedDescription]);
  }else{
      NSLog(@"removeFile 成功");
  }
}


@end
